package com.dev.security.controller;

import com.dev.security.service.UserService;
import com.dev.security.vo.PageVo;
import com.dev.security.vo.ResponseData;
import com.dev.security.vo.RoleVo;
import com.dev.security.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户控制器
 *
 * @author yuboon
 * @version v1.0
 * @date 2020/04/04
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/save")
    public ResponseEntity save(UserVo userVo){
        return new ResponseEntity(userService.save(userVo), HttpStatus.OK);
    }

    @PostMapping("/update")
    public ResponseEntity update(UserVo userVo){
        return new ResponseEntity(userService.update(userVo), HttpStatus.OK);
    }

    @PostMapping("/remove")
    public ResponseEntity remove(int id){
        return new ResponseEntity(userService.remove(id), HttpStatus.OK);
    }

    @PostMapping("/batchRemove")
    public ResponseEntity batchRemove(String ids){
        return new ResponseEntity(userService.batchRemove(ids), HttpStatus.OK);
    }

    @GetMapping("/list")
    public ResponseEntity list(UserVo userVo, PageVo page){
        ResponseData<List<UserVo>> result = new ResponseData<>();
        PageVo<UserVo> pageVo = userService.selectList(userVo,page);
        result.setCount(pageVo.getTotal());
        result.setData(pageVo.getList());
        //return new ResponseEntity(userService.selectList(userVo), HttpStatus.OK);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/assignRole")
    public ResponseEntity assignRole(int userId, String roleIds){
        return ResponseEntity.ok(userService.assignRole(userId,roleIds));
    }

    @GetMapping("/selectAssignRole")
    public ResponseEntity selectAssignRole(int userId){
        ResponseData<List<RoleVo>> result = new ResponseData<>();
        result.setData(userService.selectAssignRole(userId));
        return ResponseEntity.ok(result);
    }

    @PostMapping("/updatePass")
    public ResponseEntity updatePass(String oldPassword,String newPassword){
        ResponseData result = new ResponseData<>();
        result.setCode(userService.updatePass(oldPassword,newPassword));
        return ResponseEntity.ok(result);
    }

}
